/**
 * This is a simple program which uses pipes to send commands to the interactive GP interpreter.
 **/

#include <stdio.h>
#include <stdlib.h>

FILE *stream;

int gp_start(void)
{
  stream = popen("gp", "w");

  if(!stream)
    {
      fprintf (stderr,"incorrect parameters or too many files.\n");
      return -1;
    }

  return 0;
}

void gp_eval(const char *cmd)
{
  fprintf(stream, "%s\n", cmd);

  if(ferror(stream))
    {
      fprintf(stderr, "Output to stream failed.\n");
      exit(EXIT_FAILURE);
    }
}

int gp_kill(void)
{

 if (pclose(stream) != 0)
    {
      fprintf (stderr,"error.\n");
      return -1;
    }

 return 0;
}

int main(void)
{
  gp_start();
 
  gp_eval("n=249582");
  gp_eval("factor(n)");
  gp_eval("for(i=1,10,print(prime(i)))");

  return gp_kill();
}
